$(function(){
	var userId = $('#user-id');
	var userName = $('#user-name');

	userId.blur(function() {
		var value = $(this).val();
		$.ajax({
			async   : false,
			type    : "GET",
			url     : "/a_admin/order.getUserName",
			dataType: "json",
			data    : {user_id: value},
			success : function(data) {
				if (data.error == 0) {
					var name = data.name;
					if (data.company) {
						name += ' (' + data.company + ')';
					}
					userName.val(name);
				} else {
					userName.val('');
				}
			}
		});
	});

	userName.autocomplete('/a_admin/order.autocomplete', {
		max: 10,
		matchSubset: 1,
		autoFill: false,
		matchContains: 1,
		matchSubset: 1,
		cacheLength: 10,
		maxItemsToShow: 10
	});

	userName.result(function(event, data, formatted) {
		userId.val((data && data[1] != '') ? data[1] : '');
	});
});